package com.sgb.kjwl.viewmodel.manage;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseWebsiteEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.manage.ChildForDemandAskLeaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChildForDemandAskLeaseModel extends BaseViewModel {

    private ChildForDemandAskLeaseFragment fragment;

    public ChildForDemandAskLeaseModel(ChildForDemandAskLeaseFragment fragment) {
        this.fragment = fragment;
    }

    public void getDataList(String websiteNo, String websiteType, int page) {
        Map map = new HashMap();
        if ("0".equals(websiteType)) {//个人网站
            map.put("userNo", websiteNo);
        }else{
            map.put("companyNo", websiteNo);
        }
        map.put("categoryType", "");//类型：1机械， 2 设备，3车辆，4 房产，5土地，6工厂，7农机, "":全部
        map.put("pageNum", page);
        map.put("pageSize", 10);
        DialogHelper.showProgressDialog(fragment.getContext(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getCompanyRentSeekData(map).enqueue(new Callback<BaseEntity<ProListEntity<List<AskLeaseWebsiteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<AskLeaseWebsiteEntity>>>> call, Response<BaseEntity<ProListEntity<List<AskLeaseWebsiteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        fragment.showList(response.body().getData().getList());
                    } else {
                        fragment.showList(new ArrayList<>());
//                        MToast.showToast(fragment.getActivity(), "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<AskLeaseWebsiteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(int type, String id) {

        String urlName = "";
        if (type == 1) {//机械
            urlName = "mechanicsRentDetails";
        } else if (type == 2) {//设备
            urlName = "equipmentRentDetails";
        } else if (type == 3) {//车辆
            urlName = "vehicleRentDetails";
        } else if (type == 4) {//房产
            urlName = "houseRentDetails";
        } else if (type == 5) {//土地
            urlName = "landRentDetails";
        } else if (type == 6) {//工厂
            urlName = "factoryRentDetails";
        } else if (type == 7) {//农机
            urlName = "farmRentDetails";
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    if (!TextUtils.isEmpty(url)) {
//                        WebViewX5HostActivity.start(fragment.getActivity(), url);
                        WebViewX5HostActivity.start(fragment.getActivity(), url,id,"asklease",String.valueOf(type));
                    }
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}
